//
//  PickFoodViewController.m
//  ZooNgonDoiOS
//
//  Created by Trung sound on 8/9/12.
//  Copyright (c) 2012 Personal. All rights reserved.
//

#import "PickFoodViewController.h"
#import "PickFoodTableView.h"
#import "CommentViewController.h"

@implementation PickFoodViewController
@synthesize pickFoodViewDelegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithObject:(NSString *)a {
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        [self initHeaderView];
        [self initContentView];
        [self initFooterView];
    }
    
    return self;
}

- (id)initWithObject:(NSMutableArray *)pArrayFoodChecked arrayNoneChecked:(NSMutableArray *)pArrayFoodNoneChecked {
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        _arFoodChecked = pArrayFoodChecked;
        _arFoodNoneCheck = pArrayFoodNoneChecked;
        _arFoodChecked_BK = [[NSMutableArray alloc]initWithArray:_arFoodChecked];
        _arFoodNoneCheck_BK = [[NSMutableArray alloc]initWithArray:_arFoodNoneCheck];
        [self initHeaderView];
        [self initContentView];
        [self initFooterView];
    }
    
    return self;
}

- (void)initHeaderView {
    _vHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 48)];
    _vHeader.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:IMG_NAV_BACKGROUND]];
    
    _btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 48, 48)];
    [_btnBack setBackgroundImage:[UIImage imageNamed:IMG_BACK_NORMAL] forState:UIControlStateNormal];
    [_btnBack setBackgroundImage:[UIImage imageNamed:IMG_BACK_SELECTED] forState:UIControlStateHighlighted];
    [_btnBack addTarget:self action:@selector(btnBackClicked) forControlEvents:UIControlEventTouchUpInside];
    
    _btnDone = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width * 3/4, 0, self.view.bounds.size.width/4, 48)];
    
    [_btnDone setTitle:BTN_DONE_TITLE forState:UIControlStateNormal];
    [_btnDone setFont:[UIFont boldSystemFontOfSize:15]];
    [_btnDone addTarget:self action:@selector(btnDoneClicked) forControlEvents:UIControlEventTouchUpInside];
    [_vHeader addSubview:_btnBack];
    [_vHeader addSubview:_btnDone];
    
    [self.view addSubview:_vHeader];
    
    [_btnBack release];
    [_btnDone release];
    [_vHeader release];
}

- (void)initContentView {
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_VIEW_BACKGROUND]]];
    
    _pickFoodTableView = [[PickFoodTableView alloc]initWithFrame:CGRectMake(0, 48, self.view.bounds.size.width, self.view.bounds.size.height-48-36)];
    _pickFoodTableView.arFoodNoneCheck = _arFoodNoneCheck;
    _pickFoodTableView.arFoodChecked = _arFoodChecked;
    [_pickFoodTableView setBackgroundColor:[UIColor clearColor]];
    _pickFoodTableView.parentView = self;
    [self.view addSubview:_pickFoodTableView];
    
    
}

- (void)initFooterView {
    _vFooter = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height-38, self.view.bounds.size.width, 38)];
    [_vFooter setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_PICKFOOD_FOOTER]]];
    _lbFooter = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
    [_lbFooter setText:STRING_YOU_CHOSEN];
    [_lbFooter setBackgroundColor:[UIColor clearColor]];
    [_lbFooter setTextColor:[UIColor whiteColor]];
    [_lbFooter setFont:[UIFont systemFontOfSize:15]];
    
    _btnListAte = [[UIButton alloc]initWithFrame:CGRectMake(205, 10, 110, 20)];
    [_btnListAte addTarget:self action:@selector(btnListAteClicked) forControlEvents:UIControlEventTouchUpInside];
    _lbNumOfFood = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 45, 20)];
    [_lbNumOfFood setText:[NSString stringWithFormat:@"%d",[_arFoodChecked count]]];
    [_lbNumOfFood setFont:[UIFont systemFontOfSize:15]];
    [_lbNumOfFood setBackgroundColor:[UIColor clearColor]];
    [_lbNumOfFood setTextAlignment:UITextAlignmentRight];
    [_lbNumOfFood setTextColor:[UIColor whiteColor]];
    _lbFood = [[UILabel alloc]initWithFrame:CGRectMake(50, 0, 35, 20)];
    [_lbFood setText:STRING_FOOD];
    [_lbFood setTextColor:[UIColor whiteColor]];
    [_lbFood setBackgroundColor:[UIColor clearColor]];
    [_lbFood setFont:[UIFont systemFontOfSize:15]];
    [_lbFood setTextAlignment:UITextAlignmentLeft];
    [_btnListAte addSubview:_lbNumOfFood];
    [_btnListAte addSubview:_lbFood];
    
    [_vFooter addSubview:_btnListAte];
    [_vFooter addSubview:_lbFooter];
    [self.view addSubview:_vFooter];
    
    [_lbFooter release];
    [_btnListAte release];
    [_lbNumOfFood release];
    [_lbFood release];
    [_vFooter release];
}

- (void)btnListAteClicked {
    NSLog(@"ListAteClicked");

    _customAlertView = [[CustomAlertView alloc]initWithTitle:@"Số món đã chọn" message:@"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" delegate:self cancelButtonTitle:@"Hủy" otherButtonTitles:@"Xóa", nil];
    _customAlertView.sum = [_arFoodChecked count];
    _customAlertView.arFoodChecked = _arFoodChecked;
//    [_customAlertView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_DELFOOD_BACKGROUND]]];
    [_customAlertView show];
    [_customAlertView release];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        NSMutableIndexSet *indexSet = [[NSMutableIndexSet alloc]init];

        for (NSNumber *i in _customAlertView.arFoodRemoveCheck) {
            NSLog(@"%d",[i integerValue]);
            [_arFoodNoneCheck addObject:[_arFoodChecked objectAtIndex:[i integerValue]]];
            [indexSet addIndex:[i unsignedIntegerValue]];
        }
        [_arFoodChecked removeObjectsAtIndexes:indexSet];
        [_pickFoodTableView reloadData];
        [_lbNumOfFood setText:[NSString stringWithFormat:@"%d",[_arFoodChecked count]]];
        [indexSet release];
    }
}
- (void)setForArrayFoodAte:(NSMutableArray *)arFood {
    _arFoodChecked = arFood;
    [_lbNumOfFood setText:[NSString stringWithFormat:@"%d",[_arFoodChecked count]]];
}

- (void)btnBackClicked {
//    CommentViewController *commentViewController = [[CommentViewController alloc]initWithObject:@"a"];
//    [self.navigationController pushViewController:commentViewController animated:YES];
    if (pickFoodViewDelegate!=nil) {
        [pickFoodViewDelegate pickFoodViewControllerDismissed:_arFoodChecked_BK arrayNoneCheck:_arFoodNoneCheck_BK];
    }

    [self dismissModalViewControllerAnimated:YES];
}

- (void)btnDoneClicked {
    if (pickFoodViewDelegate!=nil) {
        [pickFoodViewDelegate pickFoodViewControllerDismissed:_arFoodChecked arrayNoneCheck:_arFoodNoneCheck];
    }
    [self dismissModalViewControllerAnimated:YES];
}

- (void)pushAddFoodViewController {
    addFoodView = [[AddFoodViewController alloc]initAddFood];
    addFoodView.addFoodDelegate = self;
    [addFoodView.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_VIEW_BACKGROUND]]];
    [self presentModalViewController:addFoodView animated:YES];
}

#pragma mark - AddFoodDelegate
- (void)addFoodDone:(ZooFoodBean *)newFood state:(BOOL)stateAdd {
    [_arFoodNoneCheck addObject:newFood];
    [_arFoodNoneCheck_BK addObject:newFood];
     [_pickFoodTableView reloadData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
        // Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
        // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
